#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include "../../include/ehome.h"
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    sd=socket(PF_INET,SOCK_DGRAM,0);

    thread = new QThread(this);//改
    FaceWork = new faceThread;

    led_bedroom=0;
    led_kitchen=0;
    led_restroom=0;
    led_saloon=0;

    run_num=0;

    camera_client.connectToHost(QHostAddress("192.168.1.6"),PORT_VIDEO);
    camera_client.requestImage();
    connect(&camera_client,SIGNAL(newImageReady(QImage)),this,SLOT(showImage(QImage)));
    connect(&step_timer, SIGNAL(timeout()), this, SLOT(timerRunning()));

    //将工作对象移动到线程对象中
    FaceWork->moveToThread(thread);
    thread->start();//启动线程
    //当Face触发则执行工作线程

    //connect(&camera_client,SIGNAL(face(QImage)),FaceWork,SLOT(onFace(QImage)));
    //connect(FaceWork,SIGNAL(someone()),this,SLOT(sndwarm()));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::sendToServer(int cmd){
    int res=0;

    struct sockaddr_in addr;
    addr.sin_family=PF_INET;
    addr.sin_addr.s_addr=inet_addr(SERVERIP);
    addr.sin_port=htons(PORT_LEDS);
    res=sendto(sd,&cmd,sizeof(cmd),0,(struct sockaddr *)&addr,sizeof(addr));
    if(res<0){
        qWarning(strerror(res));
    }

}

void MainWindow::recvFromServer(void *buf,int count){
   recv(sd,buf,count,0);

}

void MainWindow::on_pushButton_bed_clicked()
{
    int cmd=LED_BEDROOM_ON;
    if(led_bedroom){
       cmd=LED_BEDROOM_OFF;
       sendToServer(cmd);
       ui->label_bed->setStyleSheet("image: url(:/images/light.bmp);");
       led_bedroom=0;
    }else{
        cmd=LED_BEDROOM_ON;
        sendToServer(cmd);
        ui->label_bed->setStyleSheet("image: url(:/images/light.jpg);");
        led_bedroom=1;
    }
}

void MainWindow::on_pushButton_saloon_clicked()
{
    int cmd=LED_SALOON_ON;
    if(led_saloon){
        cmd=LED_SALOON_OFF;
        sendToServer(cmd);
        ui->label_saloon->setStyleSheet("image: url(:/images/light.bmp);");
        led_saloon=0;
    }else{
        cmd=LED_SALOON_ON;
        sendToServer(cmd);
        ui->label_saloon->setStyleSheet("image: url(:/images/light.jpg);");
        led_saloon=1;
    }
}

void MainWindow::on_pushButton_kitchen_clicked()
{
    int cmd=LED_KITCHEN_ON;
    if(led_kitchen){
        cmd=LED_KITCHEN_OFF;
        sendToServer(cmd);
        ui->label_kitchen->setStyleSheet("image: url(:/images/light.bmp);");
        led_kitchen=0;
    }else{
        cmd=LED_KITCHEN_ON;
        sendToServer(cmd);
        ui->label_kitchen->setStyleSheet("image: url(:/images/light.jpg);");
        led_kitchen=1;
    }
}

void MainWindow::on_pushButton_rest_clicked()
{
    int cmd=LED_RESTROOM_ON;
    if(led_restroom){
        cmd=LED_RESTROOM_OFF;
        sendToServer(cmd);
        ui->label_rest->setStyleSheet("image: url(:/images/light.bmp);");
        led_restroom=0;
    }else{
        cmd=LED_RESTROOM_ON;
        sendToServer(cmd);
        ui->label_rest->setStyleSheet("image: url(:/images/light.jpg);");
        led_restroom=1;
    }
}


void MainWindow::showImage(QImage image){
    ui->label->setPixmap(QPixmap::fromImage(image));
}

void MainWindow::sndwarm()
{
    disconnect(FaceWork, 0, this, 0);
    int cmd=BEEP_ON;
    sendToServer(cmd);
    QThread::sleep(1);
    cmd=BEEP_OFF;
    sendToServer(cmd);
    QThread::msleep(500);
    connect(FaceWork,SIGNAL(someone()),this,SLOT(sndwarm()));

}

void MainWindow::on_pushButton_beep_clicked()
{
    int cmd=BEEP_ON;
    static int status=0;
    cmd+=status;
    status=status?0:1;
    sendToServer(cmd);
    if(cmd==BEEP_ON){
        ui->pushButton_beep->setText("关报警");
    }else if(cmd==BEEP_OFF){
        ui->pushButton_beep->setText("开报警");
    }
}

void MainWindow::on_pushButton_prev_clicked()
{
    int cmd=MUSIC_PREV;
    sendToServer(cmd);
}

void MainWindow::on_pushButton_play_pause_clicked()
{
    static int status=0;
    int cmd=MUSIC_PLAY+status;
    status=status?0:1;
    sendToServer(cmd);
    if(cmd==MUSIC_PLAY){
        ui->pushButton_play_pause->setText("暂停");
    }else if(cmd==MUSIC_PAUSE){
        ui->pushButton_play_pause->setText("播放");
    }
}

void MainWindow::on_pushButton_next_clicked()
{

    int cmd=MUSIC_NEXT;
    sendToServer(cmd);
}

void MainWindow::on_pushButton_startstep_clicked()
{
    if(!step_timer.isActive()){
        step_timer.start(100);
        ui->pushButton_startstep->setText("stop");
    }else{
        step_timer.stop();
        ui->pushButton_startstep->setText("start");
        run_num=0;
        ui->label_stepnum->clear();
    }
}

void MainWindow::timerRunning()
{   char numString[30];
    int flag;
    int cmd=IF_MONITOR;
    sendToServer(cmd);
    recvFromServer(&flag,sizeof(flag));
    run_num+=flag;
    sprintf(numString,"%d",run_num);
    ui->label_stepnum->setText(numString);
}

void MainWindow::on_pushButton_kernel_clicked()
{
    int status;
    int cmd=UPDATE_KERNEL;
    sendToServer(cmd);
    recvFromServer(&status,sizeof(status));

    if(status){
        QMessageBox::warning(this,"提示","failed to update kernel");
    }else{
        QMessageBox::information(this,"提示","success to update kernel");
    }
}

void MainWindow::on_pushButton_server_clicked()
{
    int status;
    int cmd=UPDATE_SERVER;
    sendToServer(cmd);
    recvFromServer(&status,1);

    if(status){
        QMessageBox::warning(this,"提示","failed to update server");
    }else{
        QMessageBox::information(this,"提示","success to update server");
    }
}

void MainWindow::on_pushButton_client_clicked()
{
    int status;
    int cmd=UPDATE_CLIENT;
    sendToServer(cmd);
    recvFromServer(&status,sizeof(status));
    printf("QT:the received status is %d",status);
    if(status){
        QMessageBox::warning(this,"提示","failed to update client");
    }else{
        QMessageBox::information(this,"提示","success to update client");
    }
}
